function HandlePageTableWeeklyWage(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  
  this._wagedate = (options && options.wage) ? options.wage : null;
  this._createElements();
};

HandlePageTableWeeklyWage.prototype._createElements = function() {
  var _self = this;
  this._popupBox = new PopupBox();
  this._popupBox2 = new PopupBox2();
  this._hourlyRate = 0;
  
  tr = this._gui.mains.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.backgroundColor = '#fff';
  td.colSpan = 9;
  td.appendChild(DOMUtils.getLoadingImage());
  
  this._loadData();
};

HandlePageTableWeeklyWage.prototype._loadData = function() {
  this._wages = null;
  this._users = null;
  this._parameters = null;
  this._retrieveWages(1);
  this._retrieveUsers();
};

HandlePageTableWeeklyWage.prototype._verifyData = function() {
  if (this._wages && this._users) {
    this._updateElements();
  }
};

HandlePageTableWeeklyWage.prototype._retrieveWages = function(page, condition, pos) {
  var _self = this;
  var cond = '(d.isWeeklyPay = 1) AND (d.particular LIKE \'%Wage%\')' + ' AND d.weeklyWageId =' + this._wagedate.id;
  var args = ((condition) ? '&c=' + condition : '&c=' + cond) + '&p=' + page + '&s=' + DlmanageUtils.number + ((this._wage && this._wage.date) ?  '&date=' + JSON.stringify(this._wage.date) : '');
  new RequestUtils()._custom('asstwages', args, function(result, params) { _self._wages = result.data;
                                                                           _self._parameters = result;
                                                                           _self._verifyData.call(_self);
                                                                         }, { pos: (pos) ? pos : null });
};

HandlePageTableWeeklyWage.prototype._retrieveUsers = function() {
  var _self = this;
  new RequestUtils()._read('user', null, null, null, null, 'd.type', null, function(result, params) { _self._users = result.data;
                                                                                                      _self._verifyData.call(_self);
                                                                                                    }, null);
};

HandlePageTableWeeklyWage.prototype._updateElements = function() {
  var _self = this, unique = MiscUtils.unique();
  DOMUtils.removeChildElements(this._gui.pagination);
  DOMUtils.removeTableRows(this._gui.mains, 1);

  if (this._wages.length == 0) {
    tr = this._gui.mains.insertRow(-1);
    tr.className = 'roweven';
    td = tr.insertCell(-1);
    td.colSpan = 9;
    td.style.height = '24px';
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._wages.length; i < il; i++) {
    var wage = this._wages[i];
    tr = this._gui.mains.insertRow(-1);
    tr.style.backgroundColor = (tr.rowIndex % 2 == 0) ? '#ECECE4' : '#F5F5EC';
    
    td = tr.insertCell(-1);
    td.style.height = '25px';
    td.style.textAlign = 'center';
    if (wage.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(SimpleDate.format(wage.createdDate)));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    if (wage.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(SimpleDate.format(wage.payDate)));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 12px';
    if (wage.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(wage.payee));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 12px';
    if (wage.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode(wage.particular));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    if (wage.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    td.appendChild(document.createTextNode('$' + wage.amount));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    if (wage.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    var exist = false;
    for(var j = 0, jl = this._users.length; j < jl; j++) {
      var u = this._users[j];
      if (u.account == wage.payee && u.skill.employmentType == 2) {
        this._hourlyRate = u.skill.hourlyRate;
      } else if (u.account == wage.payee && u.skill.employmentType == 1) {
        exist = true;
      }
    }
    if (exist) {
      td.appendChild(document.createTextNode(''));
    } else {
      td.appendChild(document.createTextNode((this._hourlyRate) ? wage.amount / this._hourlyRate : ''));
    }
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 12px';
    if (wage.status == -1) {
      td.style.textDecoration = 'line-through';
    }
    if (wage.note && wage.note.length > 40) {
      td.appendChild(document.createTextNode(wage.note.substring(0, 40) + '...'));
      var a = document.createElement('a');
      a.href = '#';
      a._wage = wage;
      a.appendChild(document.createTextNode('All'));
      a.onclick = function() { MiscUtils.alert(_self._popupBox, DOMUtils.findPos(this), this._wage.note); };
      td.appendChild(a);
    } else {
      td.appendChild(document.createTextNode(wage.note));
      if (wage.note && wage.note.length == 0) {
        td.appendChild(document.createTextNode('(none)'));
      }
    }
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    var remove = document.createElement('img');
    remove.src = 'images/common/delete.png';
    remove.style.cursor = 'pointer';
    remove._wage = wage;
    remove.onclick = function() { var pos = DOMUtils.findPos(this);
                                  var div = document.createElement('div');
                                  var wage = this._wage;
                                  var date = _self._wagedate;
                                  var func1 = function() { wage.status = -1;
                                                           wage.cancelledDate = _self._now;
                                                           wage.cancelledOperator = _self._operator.account;
                                                           wage.cancelledMessage = wage._text;
                                                           date.amount = date.amount - wage.amount;
                                                           var args = '&date=' + JSON.stringify(date) + '&exp=' + JSON.stringify([wage]);
                                                           new RequestUtils()._custom('weeklywage1', args, function() { _self._retrieveWages(1); }, { pos: DOMUtils.findPos(this) });
                                                           _self._popupBox2._close();
                                                         };
                                  var func2 = function() { _self._popupBox2._close(); };
                                  MiscUtils.dialog(_self._popupBox2, pos, div, func1, func2, {t: 'Cancel Reason'});
                                  new DialogInput(div, wage, { title: '' });
                                  return false;
                                };
    if (wage.status != -1) {
      td.appendChild(remove);
    }
  }

  Pagination.makePagedResults(this._gui.pagination, this._parameters.page, this._parameters.total, this._parameters.size, function(page, condition) { _self._retrieveWages.call(_self, page, _self._parameters.condition); }, this, document);
  
};


HandlePageTableWeeklyWage.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

HandlePageTableWeeklyWage.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

HandlePageTableWeeklyWage.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};
